package xyz.scootaloo.oj.leetcode

import cn.hutool.core.lang.Console
import org.junit.Test
import xyz.scootaloo.console.app.support.Tester

/**
 * @author flutterdash@qq.com
 * @since 2021/4/30 22:16
 */
class P34 {

    fun searchRange(nums: IntArray, target: Int): IntArray {
        if (nums.isEmpty())
            return intArrayOf(-1, -1)
        val pivot = nums.binarySearch(target)
        if (pivot < 0)
            return intArrayOf(-1, -1)

        var l = pivot
        var r = pivot
        val limit = nums.size - 1
        while (l > 0 && nums[l - 1] == target)
            l--
        while (r < limit && nums[r + 1] == target)
            r++
        return intArrayOf(l, r)
    }

    @Test
    fun test0() {
        val input = intArrayOf(5, 7, 7, 8, 8, 10)
        Console.log(searchRange(input, 8))
    }

    @Test
    fun test1() {
        val input = intArrayOf(5, 7, 7, 8, 8, 10)
        Console.log(searchRange(input, 6))
    }

}
